package com.qf.service.impl;

import com.qf.mapper.GoodsMapper;
import com.qf.pojo.Goods;
import com.qf.service.GoodsService;
import com.qf.util.QNutil;
import com.qf.util.ResultMsg;
import com.qf.vo.VoGoods;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.util.Date;
import java.util.List;

@Service
public class GoodsServiceImpl  implements GoodsService {

    @Autowired
    GoodsMapper goodsMapper;
    @Override
    public ResultMsg selectAll(int page,int limit) {
        List<Goods> glist= goodsMapper.selectAll((page-1)*limit,limit);

        ResultMsg resultMsg=new ResultMsg();

        resultMsg.setData(glist);
        resultMsg.setCount(60);

        return resultMsg;
    }

    @Override
    public ResultMsg selectAllBySearch(Date from, Date to) {
        List<Goods> glist= goodsMapper.selectAllBySearch(from,to);
        ResultMsg resultMsg=new ResultMsg();

        resultMsg.setData(glist);
        return resultMsg;
    }

    @Override
    public ResultMsg deleteById(int gid) {
        int res=goodsMapper.delete(gid);
        ResultMsg resultMsg=new ResultMsg();
        if(res==1){
            resultMsg.setCode(200);
        }
        return resultMsg;
    }

    @Override
    public ResultMsg selectOne(int gid) {
        Goods goods=goodsMapper.selectOne(gid);
        ResultMsg resultMsg=new ResultMsg();
        if(goods!=null){
            resultMsg.setData(goods);
        }
        return resultMsg;
    }

    @Override
    public ResultMsg update(VoGoods goods) {
        ResultMsg resultMsg=new ResultMsg();
        if(goods.getGid()==null){
            //新增
            int res =goodsMapper.insert(goods);
            if (res == 1) {
                resultMsg.setCode(200);
            }
        }else {
            //修改
            int res = goodsMapper.update(goods);
            if (res == 1) {
                resultMsg.setCode(200);
            }
        }
        return resultMsg;
    }

    @Override
    public ResultMsg upload(MultipartFile file) {
        String url=QNutil.upload(file);

        ResultMsg resultMsg=new ResultMsg();
        if(url!=null){
            resultMsg.setCode(200);
            resultMsg.setData(url);
        }
        return resultMsg;
    }
}
